package weaver.jobs.camci;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.util.List;

/**
 * @author dusongsong
 * @date 2023年12月21日 14:15
 */
public class WeaSyncWebTravelCronJob extends BaseCronJob {
    @Override
    public void execute() {
        try {
            BaseBean baseBean = new BaseBean();
            String tablename = "uf_xcbb";
            baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>date=" + DateUtil.getFullDate());
            String currentDate = DateUtil.getCurrentDate();
            JSONObject paramJson = new JSONObject();
            paramJson.put("sign", CamciUtil.getSign());
            paramJson.put("sta_date", currentDate);
            paramJson.put("end_date", currentDate);
            paramJson.put("requestId", "");
            JSONObject resultJson = CamciUtil.sendOKHttpRequest(paramJson);
            baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equalsIgnoreCase(code)) {
                String dataStr = Util.null2String(resultJson.getString("data"));
                JSONObject dataJson = JSONObject.parseObject(dataStr);
                String type = Util.null2String(dataJson.getString("type"));
                if ("ok".equalsIgnoreCase(type)) {
                    String dataResultStr = Util.null2String(dataJson.getString("data"));
                    JSONObject dataResultJson = JSONObject.parseObject(dataResultStr);
                    String detailStr = Util.null2String(dataResultJson.getString("detail"));
                    if (CamciUtil.isJSONArray(detailStr)) {
                        JSONArray detailList = JSONArray.parseArray(detailStr);
                        JSONObject mainDataJson = new JSONObject();
                        mainDataJson.put("txr", "1");
                        for (int i = 0; i < detailList.size(); i++) {
                            JSONObject detailJson = detailList.getJSONObject(i);
                            baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>detailJson=" + detailJson);
                            String sta_date = Util.null2String(detailJson.getString("sta_date"));
                            String end_date = Util.null2String(detailJson.getString("end_date"));
                            String end_areas = Util.null2String(detailJson.getString("end_areas"));
                            String emp_no = Util.null2String(detailJson.getString("emp_no"));
                            String task_desc = Util.null2String(detailJson.getString("task_desc"));
                            String userid =CamciUtil.getResourceidByLoginid(emp_no);
                            if(!"".equals(sta_date) &&!"".equals(end_date) && !"".equals(userid)){
                                sta_date = sta_date.substring(0,10);
                                end_date = end_date.substring(0,10);
                                JSONObject statusJson = CamciUtil.isExistResourceid(userid,tablename+"_dt1",sta_date,end_date,end_areas);
                                baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>statusJson=" + statusJson);
                                String status = Util.null2String(statusJson.getString("status"));
                                String mainid = Util.null2String(statusJson.getString("mainid"));
                                baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>status=" + status);
                                if("I".equalsIgnoreCase(status)){
                                    int dataid = CamciUtil.insertModelData(tablename, mainDataJson);
                                    baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>插入人员dataid="+dataid);
                                    List<LocalDate> dateList = CamciUtil.splitByDateBetween(sta_date,end_date);
                                    for (LocalDate date:dateList) {
                                        JSONObject dateJson = new JSONObject();
                                        dateJson.put("bbr",userid);
                                        dateJson.put("xcrq",date.toString());
                                        dateJson.put("sx","7");
                                        dateJson.put("qtdd",end_areas);
                                        dateJson.put("nrdj",task_desc);
                                        dateJson.put("mainid",dataid);
                                        CamciUtil.insertDetailData(tablename+"_dt1",dateJson);
                                    }
                                }
                                if("U".equalsIgnoreCase(status)){
                                    baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>更新人员>>>mainid="+mainid);
                                    boolean flag = CamciUtil.deleteDetailData(tablename+"_dt1",mainid);
                                    if(flag){
                                        List<LocalDate> dateList = CamciUtil.splitByDateBetween(sta_date,end_date);
                                        for (LocalDate date:dateList) {
                                            JSONObject dateJson = new JSONObject();
                                            dateJson.put("bbr",userid);
                                            dateJson.put("xcrq",date.toString());
                                            dateJson.put("sx","7");
                                            dateJson.put("qtdd",end_areas);
                                            dateJson.put("nrdj",task_desc);
                                            dateJson.put("mainid",mainid);
                                            CamciUtil.insertDetailData(tablename+"_dt1",dateJson);
                                        }
                                    }
                                }
                               if("N".equalsIgnoreCase(status)){
                                   baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>重复且跳过的人员>>>mainid="+mainid);
                               }
                            }else {
                                baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>人员不存在｜开始日期为空｜结束日期为空");
                            }
                        }
                    }else {
                        baseBean.writeLog(">>>>>WeaSyncWebTravelCronJob>>>>>空数组导致数组格式转化异常");
                    }

                }
            }
        } catch (Exception e) {
            System.out.println(">>>>>WeaSyncWebTravelCronJob>>>Exception:" + e);
        }
    }
}
